#include <cstdio>
#include <cstdlib>
#include <windows.h>

#include <array.h>
#include <link.h>
#include <poly.h>

void setup() {
    // 设置控制台代码页为UTF-8
    SetConsoleOutputCP(CP_UTF8);
    // 设置控制台输入代码页为UTF-8
    SetConsoleCP(CP_UTF8);
    setvbuf(stdout, NULL, _IONBF, 0); // 设置为无缓冲模式
}

int array_combine() {
    // 拟两组数据
    SqList L1, L2;
    SqList_Init(L1);
    SqList_Init(L2);

    int pre_array1[] = {17, 2, 8, 6, 5, 11, 12, 1};
    int pre_array2[] = {9, 6, 10, 9, 3, 7, 13};

    // 将预定义的数据存入线性表
    for (int i = 0; i < 8; i++) {
        SqList_Insert(L1, i + 1, pre_array1[i]);
    }
    for (int i = 0; i < 7; i++) {
        SqList_Insert(L2, i + 1, pre_array2[i]);
    }

    // 排序
    SqList_Sort(L1);
    SqList_Sort(L2);

    // 打印
    printf("Here are Values:\nL1: ");
    SqList_Print(L1);
    printf("\nL2: ");
    SqList_Print(L2);
    printf("\n");

    // 合并
    SqList L;
    SqList_Combine(L1, L2, L);

    printf("Here are combine Values:\nL: ");
    SqList_Print(L);
    printf("\n");

    printf("Duplicate removal Result:\nL: ");
    SqList_Duplicate(L);
    SqList_Print(L);

    return 0;
}

int array_normal() {
    // 初始化线性表
    SqList L;
    SqList_Init(L);

    int init_length = 0;
    printf("Please input the length: ");
    scanf("%d", &init_length);

    printf("Please input the Values:\n");

    // 读入部分
    for (int i = 0; i < init_length; i++) {
        int value = 0;

        // 假定 ElemType 为 int
        scanf("%d", &value);
        SqList_Insert(L, i + 1, value);
    }

    // 输出内部元素
    printf("Here is your input Values:\n");
    SqList_Print(L);
    printf("\n");

    // 插入元素
    int insert_position = 0, elem_value = 0;
    printf("Please input the insert position: ");
    scanf("%d", &insert_position);
    printf("Please input the insert elem: ");
    scanf("%d", &elem_value);

    if (SqList_Insert(L, insert_position, elem_value) == OK) {
        // 输出内部元素
        printf("Here is your input Values:\n");
        SqList_Print(L);
        printf("\n");
    } else {
        printf("No, This unreachable position. Joker.\n");
    }


    // 删不起，真的删不起
    int delete_position = 0, delete_elem = 0;
    printf("Please input the delete position: ");
    scanf("%d", &delete_position);
    if (SqList_Delete(L, delete_position, delete_elem) == OK) {
        printf("You deleted elem: %d\n", delete_elem);
    } else {
        printf("You CANNOT Delete THIS ELEM at input POSITION.\n");
    }

    // 输出内部元素
    printf("Here is your input Values:\n");
    SqList_Print(L);
    printf("\n");

    // 搜索
    int search_elem = 0;
    printf("Please input find elem: \n");
    scanf("%d", &search_elem);
    int search_pos = SqList_Search(L, search_elem);

    if (search_pos == 0) {
        printf("Oops! NOT FOUND.\n");
    } else {
        printf("The elem at position %d.\n", SqList_Search(L, search_elem));
    }

    printf("Now Test Combine.\n");

    array_combine();


    return 0;
}

int link_normal() {
    LinkList L;

    Link_Create(L);

    // 获取用户输入
    int init_length = 0;
    printf("Please input the length: ");
    scanf("%d", &init_length);

    printf("Please input the Values:\n");

    // 读入部分
    for (int i = 0; i < init_length; i++) {
        int value = 0;

        // 假定 ElemType 为 int
        scanf("%d", &value);
        Link_Insert(L, i + 1, value);
    }

    printf("Here is your input Values:\n");
    Link_Print(L);
    printf("\n");

    // 插入元素
    int insert_position = 0, elem_value = 0;
    printf("Please input the insert position: ");
    scanf("%d", &insert_position);
    printf("Please input the insert elem: ");
    scanf("%d", &elem_value);

    if (Link_Insert(L, insert_position, elem_value) == OK) {
        // 输出内部元素
        printf("Here is your input Values:\n");
        Link_Print(L);
        printf("\n");
    } else {
        printf("No, This unreachable position. Joker.\n");
    }

    // 删不起，真的删不起
    int delete_position = 0, delete_elem = 0;
    printf("Please input the delete position: ");
    scanf("%d", &delete_position);
    if (Link_Delete(L, delete_position, delete_elem) == OK) {
        printf("You deleted elem: %d\n", delete_elem);
    } else {
        printf("You CANNOT Delete THIS ELEM at input POSITION.\n");
    }

    printf("Here is link's Values:\n");
    Link_Print(L);
    printf("\n");

    // 搜索
    int search_elem = 0;
    printf("Please input find elem: \n");
    scanf("%d", &search_elem);
    int search_pos = Link_Search(L, search_elem);

    if (search_pos == 0) {
        printf("Oops! NOT FOUND.\n");
    } else {
        printf("The elem at position %d.\n", Link_Search(L, search_elem));
    }


    return 0;
}

int link_combine() {
    // 拟两组数据
    LinkList L1, L2;
    Link_Create(L1);
    Link_Create(L2);

    int pre_array1[] = {17, 2, 8, 6, 5, 11};
    int pre_array2[] = {9, 6, 10, 9, 3, 7, 13};

    // 将预定义的数据存入线性表
    for (int i = 0; i < 6; i++) {
        Link_Insert(L1, i + 1, pre_array1[i]);
    }
    for (int i = 0; i < 7; i++) {
        Link_Insert(L2, i + 1, pre_array2[i]);
    }

    // 排序
    Link_Sort(L1);
    Link_Sort(L2);

    // 打印
    printf("Here are Values:\nL1: ");
    Link_Print(L1);
    printf("\nL2: ");
    Link_Print(L2);
    printf("\n");

    // 合并
    Link_Combine(L1, L2);
    printf("Here are Values:\nL: ");
    Link_Print(L1);

    return 0;
}


int link_pm_test() {
    PolyNomial poly1 = NULL, poly2 = NULL;

    printf("Creating first polynomial:\n");
    CreatPolynList(poly1, 3);
    printf("First polynomial: ");
    DisplayPoly(poly1);

    printf("\nCreating second polynomial:\n");
    CreatPolynList(poly2, 2);
    printf("Second polynomial: ");
    DisplayPoly(poly2);

    printf("\nAdding polynomials:\n");
    PolyAdd(poly1, poly2);
    printf("Result: ");
    DisplayPoly(poly1);

    return 0;
}

int _main() {
    printf("=========== Chioce One for Testing ===========\n");
    printf("Array: 1 - Test All   2 - Test Sort & Combine\n");
    printf("Link: 3 - Test All\n");
    printf("Polynomial merging: 4 - Test\n");
    printf("=========== ====================== ===========\n");

    int chioce = 0;
    scanf("%d", &chioce);

    if (chioce == 1) {
        array_normal();
    } else if (chioce == 2) {
        array_combine();
    } else if (chioce == 3){
        link_normal();
    } else if (chioce == 4){
        link_pm_test();
    } else {
        printf("Nothing for you.\n");
    }

    printf("\nbye!\n");
    system("pause");

    return 0;
}
